/*
 * TilesetTest.hpp
 *
 *  Created on: 04/02/2012
 *      Author: Tales
 */

#ifndef TILESETTEST_HPP_
#define TILESETTEST_HPP_

#include "Test.hpp"
#include "engine.hpp"
#include <SDL_image.h>
#include "errors.hpp"
#include <SDL.h>
#include "sprite.hpp"
#include "actor.hpp"
#include "Tilemap.hpp"

namespace wolf {

/**
 * @brief
 * Tests if the tileset is ok
 */
class TilesetTest: public wolf::Test {
public:
	TilesetTest() :
			Test("Tests the tilesets.") {
	}

	bool run() {
		const int SCREEN_WIDTH = 800;
		const int SCREEN_HEIGHT = 600;
		Engine engine(Engine::DEBUG);
		engine.setVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0);
		engine.setFrameRate(30);

		engine.start();

		SDL_Surface *tilesetSurface = IMG_Load("resource/tilesettest2.png");
		WOLF_ASSERT(tilesetSurface);

		// Just to show the tilleset.
		Sprite tilesetTipSprite(tilesetSurface, 1, 1);
		Actor tilesetTip(tilesetTipSprite,
				SCREEN_WIDTH - tilesetTipSprite.getWidth(), 0, 100);
//		engine.addLayer(tilesetTip);
		//The tilemap
		Sint16 tiles[8 * 8] = {
		//      0000  0001  0002  0003  0004  0005  0006  0007
				0x01, 0x01, 0x01, 0x05, 0x06, 0x01, 0x01, 0x01, //0
				0x01, 0x03, 0x04, 0x09, 0x0A, 0x03, 0x04, 0x01, //1
				0x01, 0x07, 0x08, 0x01, 0x03, 0x0F, 0x10, 0x04, //2
				0x01, 0x0B, 0x0C, 0x01, 0x0F, 0x07, 0x08, 0x10, //3
				0x02, 0x02, 0x02, 0x02, 0x07, 0x0C, 0x0C, 0x08, //4
				0x02, 0x01, 0x01, 0x02, 0x0C, 0x0B, 0x0C, 0x0C, //5
				0x02, -0x1, -0x1, 0x02, 0x02, 0x02, 0x02, 0x02, //6
				-0x1, -0x1, -0x1, -0x1, -0x1, -0x1, -0x1, -0x1, //7
				};
		Tilemap theTileMap(tilesetSurface, 4, 4, 8, 8, tiles, 1, 2);
		Uint16 animation[1 * 2] = {
		// 		0000  0001
				0x0D, 0x0E, // -1
				};
		theTileMap.setAnimation(animation);
		theTileMap.setAnimationSpeed(.125f);
		theTileMap.setX(16);
		theTileMap.setY(16);
//		theTileMap.setOffsetX(64);

//		engine.addLayer(theTileMap);

		//Controller
		MyEventHandler handler(theTileMap);
		engine.addManager(handler);

		while (engine.isRunning()) {
			engine.doFrame();
		}
		engine.halt();

		SDL_FreeSurface(tilesetSurface);
		return true;
	}

	class MyEventHandler: public Manager {
		Tilemap &tilemap;
	public:
		MyEventHandler(Tilemap &tilesetTest_) :
				tilemap(tilesetTest_) {
		}

		void onFrame(Engine &engine) {
			Uint8*keys = engine.getKeyState(NULL);
			if (keys[SDLK_UP]) {
				tilemap.setOffsetY(tilemap.getOffsetY() - 4);
			}
			if (keys[SDLK_DOWN]) {
				tilemap.setOffsetY(tilemap.getOffsetY() + 4);
			}
			if (keys[SDLK_LEFT]) {
				tilemap.setOffsetX(tilemap.getOffsetX() - 4);
			}
			if (keys[SDLK_RIGHT]) {
				tilemap.setOffsetX(tilemap.getOffsetX() + 4);
			}
		}

		bool onEvent(Engine &engine, SDL_Event& event) {
			switch (event.type) {
			case SDL_KEYDOWN: {
				switch (event.key.keysym.sym) {
				case SDLK_LCTRL:
				case SDLK_RCTRL: {
					bool horizontalLoop = tilemap.isHorizontalLoop();
					tilemap.setHorizontalLoop(!horizontalLoop);
					if (horizontalLoop)
						tilemap.setVerticalLoop(!tilemap.isVerticalLoop());
					break;
				}
				default:
					break;
				}
				break;
			}
			default:
				break;
			}
			return false;
		}

	};
};

} /* namespace wolf */
#endif /* TILESETTEST_HPP_ */
